package cate.game.event.master;

import cate.game.activity.godtree.GodTreeActivity;
import cate.game.event.GameEventListener;
import cate.game.role.Role;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class LevelChangeListener implements GameEventListener<LevelChangeEvent> {
    @Override
    public void onEvent(LevelChangeEvent gameEvent) {
        Role role = gameEvent.role;
        if (role == null) {
            return;
        }
        safe(() -> role.getRes().checkOpen(), "res checkOpen");
        safe(() -> role.getFarm().checkOpen(), "farm checkOpen");
        safe(() -> role.getPvp().checkOpen(), "pvp checkOpen");
        safe(() -> role.getHistory().checkOpen(), "history checkOpen");
        safe(() -> role.getActivity().checkOpen(), "activity checkOpen");
        role.getGame().rank.level.record(gameEvent.role, gameEvent.newLevel);
        if (role.getFarm().spiceFb.isOpen()) {
            role.getFarm().spiceFb.checkUnLock();
        }
        // 每次升级之后重置
        role.getStatus().levelUpTime = 0L;
        role.getFarm().rank.noticeUpdate();
        role.getRes().treasureTurntables.checkInit();
        role.getRes().farm.initGroundAndSeeds();
        role.getPay().gift.checkCondition();
        role.getGame().trace.role.onLevelUp(role);
        role.getActivity().getEnableList(GodTreeActivity.class).forEach(a->a.tryLevelUp(a.findConfigRow(a.level,a.kill)));
        role.getBase().invite.onLevelUp();
    }

    private void safe(Runnable runnable, String description) {
        try {
            runnable.run();
        } catch (Exception e) {
            log.error(description + "报错拉");
        }
    }
}
